﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using SSPharma.Common;
using SSPharma.Biz.DanhMuc;
using System.Collections.ObjectModel;

namespace SSPharma.View.DanhMuc
{   
    /// <summary>
    /// Interaction logic for ucMH_LapYeuCauMuaHang.xaml
    /// </summary>
    public partial class ucDS_DM_HeThong : UserControl
    {
        #region valible
        private DataTable p_tListData, p_tDataCauTruc;
        private IList<string> dsTenTruong;
        private IList<string> ListKhoachinh;
        private string FormKhoiTao;
        private string GroupName;
        private string Orderby = "";
        private ObservableCollection<dynamic> dynamicDt;
        #endregion

        #region contructor
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: contructor khởi tạo
        /// </summary>
        public ucDS_DM_HeThong()
        {
            InitializeComponent();
        }
        #endregion

        #region Event
        /// <summary>
        /// User: TAMNN 21/07/2014
        ///  Mục đích: Sự kiện load dữ liệu của usercontroll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //lấy format grid
            GetFormatGrid(txtFormName.Text);
            //load dữ liệu
            GetDataDanhMucHeThong(txtFormName.Text);

        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        ///  Mục đích: edit dữ liệu với sự kiện doubleclick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void g1_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                this.ev_SuaDuLieu();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }
        /// <summary>
        /// User: TAMNN 02/08/2014
        /// Mục đích: hàm double cho lưới con bên trong lưới lớn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void p_gLuoiCon_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                this.ev_SuaDuLieu();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }
        /// <summary>
        /// User: TAMNN 22/07/2014 
        /// Mục đích: đánh số thứ tự cho lưới
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void g1_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }
        /// <summary>
        /// User: TAMNN 02/08/2014
        /// Mục đích: hàm load số cho lưới bên trong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void p_gLuoiCon_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }
        /// <summary>
        /// User:TAMNN 30/07/2014
        /// Mục đích: gọi trong trường hợp có group dữ liệu.
        /// lấy dữ liệu các item khi được click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exp_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Type type = sender.GetType();
            if (type == typeof(Expander))
            {
                var expander = (Expander)sender;
                if (expander.Tag == null || string.IsNullOrEmpty(expander.Tag.ToString()))
                {
                    expander.Tag = "TRUE";
                    bool isexp = expander.IsExpanded;
                    var controll = expander.FindName("txtHeader");
                    Type typetextblock = controll.GetType();
                    if (typetextblock == typeof(TextBlock))
                    {
                        var textbox = (TextBlock)controll;
                        string name = textbox.Text;
                        DataGrid p_gLuoiCon = new DataGrid();
                        UtilityUIElement.GenerateColumns(p_gLuoiCon, p_tDataCauTruc);
                        string p_sFormKhoiTao = !string.IsNullOrEmpty(txtFormName.Text) ? txtFormName.Text.Substring(txtFormName.Text.LastIndexOf("_") + 1) : "";
                        string formTypeFullName = string.Format("{0}.{1}", "SSPharma.Biz.DanhMuc", "BizDanhMuc" + p_sFormKhoiTao);
                        Type TypeForm = Type.GetType(formTypeFullName, true);
                        dynamic frmmethod = Activator.CreateInstance(TypeForm);
                        DataTable p_tDataSub = frmmethod.ft_GetDataDanhMucTheoNhom(GroupName,name);// lay du lieu
                        dynamicDt = DataTableExtensions.ToDynamic(p_tDataSub);
                        ListCollectionView collection = new ListCollectionView(dynamicDt);
                        p_gLuoiCon.ItemsSource = dynamicDt;
                        p_gLuoiCon.HeadersVisibility = DataGridHeadersVisibility.Row;
                        p_gLuoiCon.RowHeaderWidth = 30;
                        p_gLuoiCon.CanUserAddRows = false;
                        p_gLuoiCon.IsReadOnly = true;
                        p_gLuoiCon.MouseDoubleClick += new MouseButtonEventHandler(p_gLuoiCon_MouseDoubleClick);
                        p_gLuoiCon.LoadingRow += new EventHandler<DataGridRowEventArgs>(p_gLuoiCon_LoadingRow);
                        Grid grid = new Grid();
                        grid.VerticalAlignment = VerticalAlignment.Stretch;
                        grid.ShowGridLines = false;
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20, GridUnitType.Auto) { } });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(2, GridUnitType.Auto) { } });
                        grid.Children.Add(p_gLuoiCon);
                        expander.Content = grid;
                    }
                   g2.CanUserAddRows = true;
                }
            }
        }
        #endregion

        #region Utility function
        /// <summary>
        /// User: TAMNN 21/07/2014
        ///  Mục đích: Hàm lấy cấu trúc định dạng của lưới
        /// </summary>
        /// <param name="_tenform"></param>
        private void GetFormatGrid(string _tenform)
        {
            try
            {
                dsTenTruong = new List<string>();
                ListKhoachinh = new List<string>();
                FormKhoiTao = "";
                Orderby = "";
                GroupName = string.Empty;
                BizDanhMucHeThong _DanhMucHeThong = new BizDanhMucHeThong();
                p_tDataCauTruc = _DanhMucHeThong.ft_DSDinhDang_DanhMuc(_tenform);// lay cau truc bang hien thi
                GroupName = p_tDataCauTruc.Rows[0]["Nhom"].ToString();
                FormKhoiTao = p_tDataCauTruc.Rows[0]["FormKhoiTao"].ToString();
                Orderby = p_tDataCauTruc.Rows[0]["SapXep"].ToString();
                if(string.IsNullOrEmpty(GroupName))
                {
                    UtilityUIElement.GenerateColumns(g1, p_tDataCauTruc, ref dsTenTruong, ref ListKhoachinh);
                    g2.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    UtilityUIElement.GenerateColumns(g2, p_tDataCauTruc, ref dsTenTruong, ref ListKhoachinh);
                    g1.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch (Exception ex) { //MessageBox.Show(ex.Message.ToString()); 
            }

        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Lấy dữ liệu tương ứng với từng controll
        /// </summary>
        /// <param name="_tenform"></param>
        private void GetDataDanhMucHeThong(string _tenform)
        {
            try
            {
                string p_sFormKhoiTao = !string.IsNullOrEmpty(_tenform)?_tenform.Substring(_tenform.LastIndexOf("_")+1):"";
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.Biz.DanhMuc","BizDanhMuc"+ p_sFormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                p_tListData = frmmethod.ft_GetListdata(dsTenTruong, Orderby);// lay du lieu
                if (string.IsNullOrEmpty(GroupName))
                {
                    g1.DataContext = p_tListData.AsDataView();
                }
                else
                {
                    dynamicDt = DataTableExtensions.ToDynamic(p_tListData);
                    ListCollectionView collection = new ListCollectionView(dynamicDt);
                    collection.GroupDescriptions.Add(new PropertyGroupDescription(GroupName));
                    g2.ItemsSource = collection;
                }
            }
            catch (Exception ex) { //MessageBox.Show(ex.Message.ToString());
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Được gọi sau khi form thêm của từng model được đóng. 
        /// </summary>
        public void Search_Form()
        {
            GetFormatGrid(txtFormName.Text);
            GetDataDanhMucHeThong(txtFormName.Text);
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// lấy giá trị của nhóm khóa chính tương ứng với từng model
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        public IList<string> LayGiaTriCuaKhoaChinh(DataRowView dataRow)
        {
            IList<string> list = new List<string>();
            foreach (var i in ListKhoachinh)
            {
                list.Add(dataRow[i] != null ? dataRow[i].ToString() : "");
            }
            return list;
        }
        #endregion

        #region Các hàm cố định

        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút sửa ở trên thanh trình đơn được click
        /// </summary>
        public void ev_SuaDuLieu()
        {
            var dataRow =  g1.SelectedItem;
            if (dataRow == null)
                return;
            if (dataRow.GetType() != typeof(DataRowView))
                return;
            string Id = string.Join(";", this.LayGiaTriCuaKhoaChinh((DataRowView)dataRow).ToArray());
            if (txtFormName.Text == "HT_DM_DonVi")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type, new object[] { Id });
                frmmethod.IdentityValue += new frmDonVi.IdentityValues(Search_Form);
                frmmethod.Show();
               
            }
            else if (txtFormName.Text == "HT_DM_BoPhan")
            {
                var frmmethod = new frmBoPhan(Id);
                frmmethod.IdentityValue += new frmBoPhan.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiTien")
            {
                var frmmethod = new frmLoaiTien(Id);
                frmmethod.IdentityValue += new frmLoaiTien.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiTaiKhoan")
            {
                var frmmethod = new frmLoaiTaiKhoan(Id);
                frmmethod.IdentityValue += new frmLoaiTaiKhoan.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiKH")
            {
                var frmmethod = new frmLoaiKhachHang(Id);
                frmmethod.IdentityValue += new frmLoaiKhachHang.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Kho")
            {
                var frmmethod = new frmKho(Id);
                frmmethod.IdentityValue += new frmKho.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiCT")
            {
                dynamic frmmethod = new frmLoaiChungTu(Id); 
                frmmethod.IdentityValue += new frmLoaiChungTu.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_CacNganHang")
            {
                dynamic frmmethod = new frmNganHang(Id);
                frmmethod.IdentityValue += new frmNganHang.IdentityValues(Search_Form);
                frmmethod.Show();
            }

            else if (txtFormName.Text == "HT_DM_To")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type, new object[] { Id });
                frmmethod.IdentityValue += new frmToSanXuat.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_KheUocVay")
            {
                dynamic frmmethod = new frmKheUocVay(Id);
                frmmethod.IdentityValue += new frmKheUocVay.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_DonViTinh")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type, new object[] { Id });
                frmmethod.IdentityValue += new frmDonViTinh.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiDonViTinh")
            {
                var frmmethod = new frmLoaiDVT(Id);
                frmmethod.IdentityValue += new frmLoaiDVT.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_HoanDoiDVT")
            {
                var frmmethod = new frmHoanDoiDVT(Id);
                frmmethod.IdentityValue += new frmHoanDoiDVT.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_KhoanMuc")
            {
                var frmmethod = new frmKhoanMuc(Id);
                frmmethod.IdentityValue += new frmKhoanMuc.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_QuocTich")
            {
                var frmmethod = new frmQuocTich(Id);
                frmmethod.IdentityValue += new frmQuocTich.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Machine")
            {
                var frmmethod = new frmMayMoc(Id);
                frmmethod.IdentityValue += new frmMayMoc.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiHaoHut")
            {
                var frmmethod = new frmLoaiHaoHut(Id);
                frmmethod.IdentityValue += new frmLoaiHaoHut.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Resource")
            {
                var frmmethod = new frmResource(Id);
                frmmethod.IdentityValue += new frmResource.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_NhomNhanVien")
            {
                var frmmethod = new frmNhomNhanVien(Id);
                frmmethod.IdentityValue += new frmNhomNhanVien.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_NhanVienSale")
            {
                var frmmethod = new frmNhanVienSale(Id);
                frmmethod.IdentityValue += new frmNhanVienSale.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_NghiepVu")
            {
                var frmmethod = new frmNghiepVu(Id);
                frmmethod.IdentityValue += new frmNghiepVu.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Response")
            {
                var frmmethod = new frmResponse(Id);
                frmmethod.IdentityValue += new frmResponse.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_Menu")
            {
                var frmmethod = new frmMenu(Id);
                frmmethod.IdentityValue += new frmMenu.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_TyGiaHeader")
            {
                var frmmethod = new frmTyGia(Id);
                frmmethod.IdentityValue += new frmTyGia.IdentityValues(Search_Form);
                frmmethod.Show();
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút thêm ở trên thanh trình đơn được click
        /// </summary>
        public void ev_ThemDuLieu()
        {
            if (txtFormName.Text == "HT_DM_DonVi")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmDonVi.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_BoPhan")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmBoPhan.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_TaiKhoan")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmTaiKhoan.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiTien")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmLoaiTien.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiTaiKhoan")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmLoaiTaiKhoan.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiKH")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmLoaiKhachHang.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Kho")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmKho.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiCT")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmLoaiChungTu.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_CacNganHang")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmNganHang.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_To")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                //var t = type.GetMethod("IdentityValues").Invoke(null,null);
                //System.Reflection.FieldInfo field = type.GetField("IdentityValues", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);// System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
                //if (field != null)
                //{
                //    Delegate method = field.GetValue(this) as Delegate;

                //    if (method != null)
                //    {
                //        method.Method.Invoke(method.Target, new object[0]);
                //    }
                //}
                frmmethod.IdentityValue += new frmToSanXuat.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_KheUocVay")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmKheUocVay.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_KhuVuc")
            {
                string formTypeFullName = string.Format("{0}.{1}", "SSPharma.View.DanhMuc", FormKhoiTao);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                frmmethod.IdentityValue += new frmKhuVuc.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_DonViTinh")
            {
                var frmmethod = new frmDonViTinh();
                frmmethod.IdentityValue += new frmDonViTinh.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiDonViTinh")
            {
                var frmmethod = new frmLoaiDVT();
                frmmethod.IdentityValue += new frmLoaiDVT.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_HoanDoiDVT")
            {
                var frmmethod = new frmHoanDoiDVT();
                frmmethod.IdentityValue += new frmHoanDoiDVT.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_KhoanMuc")
            {
                var frmmethod = new frmKhoanMuc();
                frmmethod.IdentityValue += new frmKhoanMuc.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_QuocTich")
            {
                var frmmethod = new frmQuocTich();
                frmmethod.IdentityValue += new frmQuocTich.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Machine")
            {
                var frmmethod = new frmMayMoc();
                frmmethod.IdentityValue += new frmMayMoc.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_LoaiHaoHut")
            {
                var frmmethod = new frmLoaiHaoHut();
                frmmethod.IdentityValue += new frmLoaiHaoHut.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Resource")
            {
                var frmmethod = new frmResource();
                frmmethod.IdentityValue += new frmResource.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_NhomNhanVien")
            {
                var frmmethod = new frmNhomNhanVien();
                frmmethod.IdentityValue += new frmNhomNhanVien.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_NhanVienSale")
            {
                var frmmethod = new frmNhanVienSale();
                frmmethod.IdentityValue += new frmNhanVienSale.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_NghiepVu")
            {
                var frmmethod = new frmNghiepVu();
                frmmethod.IdentityValue += new frmNghiepVu.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_Response")
            {
                var frmmethod = new frmResponse();
                frmmethod.IdentityValue += new frmResponse.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_Menu")
            {
                var frmmethod = new frmMenu();
                frmmethod.IdentityValue += new frmMenu.IdentityValues(Search_Form);
                frmmethod.Show();
            }
            else if (txtFormName.Text == "HT_DM_TyGiaHeader")
            {
                var frmmethod = new frmTyGia();
                frmmethod.IdentityValue += new frmTyGia.IdentityValues(Search_Form);
                frmmethod.Show();
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút xóa ở trên thanh trình đơn được click
        /// </summary>
        public void ev_XoaDuLieu()
        {

            DataRowView dataRow = (DataRowView)g1.SelectedItem;
            if (dataRow == null)
                return;
                //throw new System.Configuration.Provider.ProviderException("Bạn cần chọn đối tượng");
            if (System.Windows.MessageBox.Show("Bạn muốn xóa dữ liệu này?", "SSPharma", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                IList<string> Id = this.LayGiaTriCuaKhoaChinh(dataRow);
                BizDanhMucHeThong _DanhMucHeThong = new BizDanhMucHeThong();
                string mesg = _DanhMucHeThong.fs_Delete_DanhMuc(txtFormName.Text, ListKhoachinh, Id);
                MessageBox.Show(mesg);
                this.GetDataDanhMucHeThong(txtFormName.Text);
            }
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút lưu ở trên thanh trình đơn được click
        /// </summary>
        public void ev_LuuDuLieu()
        {
            string t = string.Empty;
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút bỏ qua ở trên thanh trình đơn được click
        /// </summary>
        public void ev_BoQua()
        {
            string t = string.Empty;
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút in ở trên thanh trình đơn được click
        /// </summary>
        public void ev_InDuLieu()
        {
            string t = string.Empty;
        }
        /// <summary>
        /// User: TAMNN 21/07/2014
        /// Mục đích: Gọi khi nút tìm kiếm ở trên thanh trình đơn được click
        /// </summary>
        public void ev_TimKiem()
        {
            string t = string.Empty;
        }
        #endregion

    }

    public static class DataTableExtensions
    {
        public static ObservableCollection<dynamic> ToDynamic(this DataTable dt)
        {
            var dynamicDt = new ObservableCollection<dynamic>();
            foreach (DataRow row in dt.Rows)
            {
                dynamic dyn = new System.Dynamic.ExpandoObject();
                foreach (DataColumn column in dt.Columns)
                {
                    var dic = (IDictionary<string, object>)dyn;
                    dic[column.ColumnName] = row[column];
                }
                dynamicDt.Add(dyn);
            }
            return dynamicDt;
        }
    }
     
}
